﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace Youz.Common.Helper
{
    public class EmailHelper
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="EmailAddress">收件人</param>
        /// <param name="Subject">主题</param>
        /// <param name="strBody">邮件类容</param>
        /// <param name="SMTPServer">邮件服务器地址</param>
        /// <param name="SMTPServerUser">发送邮件帐号</param>
        /// <param name="SMTPServerPassword">发送邮件密码</param>
        /// <returns></returns>
        public static string SendMail(string EmailAddress, string Subject, string strBody, string SMTPServer, string SMTPServerUser, string SMTPServerPassword, int Port)
        {
            try
            {
                MailMessage mailmessage = new MailMessage();
                mailmessage.To.Add(EmailAddress);
                mailmessage.From = new MailAddress(SMTPServerUser, "", Encoding.GetEncoding(936));
                mailmessage.Subject = Subject;
                mailmessage.Body = strBody;
                mailmessage.IsBodyHtml = true;
                mailmessage.BodyEncoding = System.Text.Encoding.UTF8;
                mailmessage.Priority = System.Net.Mail.MailPriority.Normal;//优先级

                SmtpClient smtpclient = new SmtpClient();
                smtpclient.Credentials = new System.Net.NetworkCredential(mailmessage.From.Address, SMTPServerPassword);//设置发件人身份的票据  
                smtpclient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtpclient.Host = SMTPServer;
                smtpclient.Port = Port; //端口號
                smtpclient.Send(mailmessage);             
                return string.Empty;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}
